package cn.com.controller.operationController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Taste;
import cn.com.db.model.StoreTaste;
import cn.com.db.service.TasteService;
import cn.com.db.service.StoreTasteService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/taste")
public class TasteOperationController {

    @Autowired
    TasteService tasteService;
    @Autowired
    StoreTasteService storeTasteService;

    @RequestMapping(value = "/{storeId}",method = RequestMethod.GET)
    public String getTaste(@PathVariable("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeTasteService.getStoreTaste(storeId));
        dataResult.setTotal(storeTasteService.countStoreTaste(storeId));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/onTaste",method = RequestMethod.POST)
    public String onTaste(@RequestParam("storeId") String storeId,
                           @RequestParam("tasteId")String tasteId,
                           @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!tasteService.isExist(tasteId)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("口味不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreTaste storeTaste=storeTasteService.getStoreTasteOfStore(storeId,tasteId);
        Taste taste=tasteService.getTasteById(tasteId);
        if (storeTaste==null){
            storeTaste=new StoreTaste();
            storeTaste.setId(UidUtil.createUUID());
            storeTaste.setTaste(taste);
            storeTaste.setStore(storeId);
            storeTaste.setDel(false);
            storeTaste.setPrice(price);
            storeTaste.setCreated_at(TimeUtil.getNowTimestamp());
            storeTaste.setUpdated_at(TimeUtil.getNowTimestamp());
            storeTasteService.onTaste(storeTaste);
            dataResult.setData(storeTasteService.getStoreTasteById(storeTaste.getId()));
        }else if (storeTaste.getDel()){
            storeTaste.setDel(false);
            storeTaste.setPrice(price);
            storeTasteService.updateTaste(storeTaste);
            dataResult.setData(storeTasteService.getStoreTasteById(storeTaste.getId()));
        }else {
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("店铺已启用该口味");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/offTaste",method = RequestMethod.POST)
    public String offTaste(@RequestParam("id") String storeTasteId){
        DataResult dataResult=new DataResult();
        if (!storeTasteService.isAlive(storeTasteId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("口味不存在,或口味不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreTaste storeTaste= storeTasteService.getStoreTasteById(storeTasteId);
        storeTaste.setDel(true);
        storeTasteService.updateTaste(storeTaste);
        dataResult.setData(storeTaste);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String offTaste(@RequestParam("id") String storeTasteId,
                            @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!storeTasteService.isAlive(storeTasteId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("口味不存在,或口味不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreTaste storeTaste= storeTasteService.getStoreTasteById(storeTasteId);
        storeTaste.setPrice(price);
        storeTasteService.updateTaste(storeTaste);
        dataResult.setData(storeTaste);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("修改成功");
        return JsonUtil.toJsonString(dataResult);
    }

}
